/*      
 * 模块编号  
 * 功能描述 
 * 文件名 OrderController.java 
 * 作者 王磊 
 * 编写日期 2022年01月09日 
 */
package wl.demo.springcloud.controller;

import java.net.URI;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import wl.demo.springcloud.entities.CommonResult;
import wl.demo.springcloud.entities.Payment;
import wl.demo.springcloud.lb.IMyRule;

@RestController
public class OrderController
{

	public static final String PAYMENT_SRV = "http://CLOUD-PAYMENT-SERVICE";

    @Autowired
    private RestTemplate restTemplate;

    @SuppressWarnings("unchecked")
	@GetMapping("/consumer/payment") //客户端用浏览器是get请求，但是底层实质发送post调用服务端8001
    public CommonResult<Integer> create(Payment payment)
    {
        return restTemplate.postForObject(PAYMENT_SRV + "/payment",payment,CommonResult.class);
    }


    @SuppressWarnings("unchecked")
	@GetMapping("/consumer/payment/{id}")
    public CommonResult<Payment> getPayment(@PathVariable Long id)
    {
        return restTemplate.getForObject(PAYMENT_SRV + "/payment/"+id, CommonResult.class, id);
    }
    
    @Resource
    private IMyRule myRule;


    @GetMapping("/consumer/payment/lb")
    public String getPaymentLB()
    {
        ServiceInstance serviceInstance = myRule.getService("CLOUD-PAYMENT-SERVICE");
        if (serviceInstance == null) {
			return "无可用服务";
		}
        URI uri = serviceInstance.getUri();

        return restTemplate.getForObject(uri+"/payment/lb",String.class);
    }
}
